<?php

class RegistrationController extends Zend_Controller_Action {
	
	public function newRegistrationAction() {
		
		$email = $this->_getParam("email");
		$password = $this->_getParam("password");
		
		if ($email === null || $password === null) {
			$output = <<<XML
<output>
	<result>
		false
	</result>
	<error>
		<code>
			0
		</code>
		<message>
			Email or password not given.
		</message>
	</error>
</output>
XML;
		} else {
			$options = array(
				"deep" => true,
				"mx" => true
			);
			$validate_email = new Zend_Validate_EmailAddress($options);
			if (!$validate_email->isValid($email)) {
				$output = <<<XML
<output>
	<result>
		false
	</result>
	<error>
		<code>
			1
		</code>
		<message>
			Email not valid.
		</message>
	</error>
</output>
XML;
			} else {
				$options = array(
					"field" => "email",
					"table" => "User"
				);
				$validate_db_no_record_exists = new Zend_Validate_Db_NoRecordExists($options);
				if (!$validate_db_no_record_exists->isValid($email)) {
					$output = <<<XML
<output>
	<result>
		false
	</result>
	<error>
		<code>
			2
		</code>
		<message>
			Email already registered.
		</message>
	</error>
</output>
XML;
				} else {
					if (strlen($password) != 64 || strcmp($password, trim($password)) != 0) {
						$output = <<<XML
<output>
	<result>
		false
	</result>
	<error>
		<code>
			3
		</code>
		<message>
			Password not valid.
		</message>
	</error>
</output>
XML;
					} else {
						$application_model_user = new Application_Model_User();
						if (!$application_model_user->insertUser($email, $password)) {
							$output = <<<XML
<output>
	<result>
		false
	</result>
	<error>
		<code>
			500
		</code>
		<message>
			Internal server error
		</message>
	</error>
</output>
XML;
						} else {
							$output = <<<XML
<output>
	<result>
		true
	</result>
</output>
XML;
						}
					}
				}
			}
		}
		
		exit($output);
		
	}
	
}